let express = require("express");
let router = express.Router();
let db = require("../db/index");
const {query} = require("express");
router.post('/add', function (req, res) {
  let {article_id, talk_id, user_id, content, creationTime} = req.body;
  db.query('INSERT INTO comment set ?', {article_id: article_id || null, talk_id: talk_id || null, user_id, content, creationTime}, function (err) {
    if (!err) {
      res.send({
        code: 200,
        message: '成功'
      })
    } else {
      res.send({
        code: 20029,
        message: err.message
      })
    }
  })
})

router.post('/reply/add', function (req, res) {
  let {replycomment_id, user_id, content, creationTime} = req.body;
  db.query('INSERT INTO comment_reply set ?', {replycomment_id, user_id, content, creationTime}, function (err) {
    if (!err) {
      res.send({
        code: 200,
        message: '成功'
      })
    } else {
      res.send({
        code: 20029,
        message: err.message
      })
    }
  })
})

router.get('/get', function (req, res) {
  let {article_id, talk_id, pageNo, limit} = req.query;
  let start = (pageNo - 1) * limit
  let sql = '';
  if (article_id) {
    sql = `SELECT c.*,a.name FROM comment c LEFT JOIN account a ON c.user_id = a.id WHERE article_id = '${article_id}' ORDER BY id DESC LIMIT ${start},${limit}`;
  }
  if (talk_id) {
    sql = `SELECT c.*,a.name FROM comment c LEFT JOIN account a ON c.user_id = a.id WHERE talk_id = '${talk_id}' ORDER BY id DESC LIMIT ${start},${limit}`;
  }
  db.query(sql, async function (err, rows) {
    if (!err) {
      let result = await Promise.all(
          rows.map(item => {
            return new Promise(resolve => {
              db.query(`SELECT c.*,a.name FROM comment_reply c LEFT JOIN account a ON c.user_id = a.id WHERE c.replycomment_id = '${item.id}'`,
                  function (err2, comment_reply) {
                    item.comment_reply = comment_reply;
                    resolve(item);
                  })
            })
          })
      )
      res.send({
        code: 200,
        comments: result
      })
    } else {
      res.send({
        code: 5001,
        message: err.message
      })
    }
  })
})

router.get('/get/all/article', function (req, res) {
  let {pageNo, limit} = req.query;
  let start = (pageNo - 1) * limit
  db.query(`select c.*,a.name from comment c JOIN account a ON c.user_id = a.id WHERE c.article_id is not null ORDER BY c.id DESC LIMIT ${start},${limit}`, async function (err, rows) {
    let comments = await Promise.all(
        rows.map(item => new Promise(resolve => {
          db.query(`select c.*,a.name from comment_reply c JOIN account a ON c.user_id = a.id WHERE replycomment_id = ${item.id}`, function (err2, rows2) {
            item.reply = rows2;
            resolve(item)
          })
        }))
    )
    db.query('select count(id) as total from comment WHERE article_id is not null', function (err, [{total}]) {
      res.send({
        code: 200,
        message: 'success',
        comments,
        total
      })
    })
  })
})

router.get('/get/all/talk', function (req, res) {
  let {pageNo, limit} = req.query;
  let start = (pageNo - 1) * limit
  db.query(`select c.*,a.name from comment c JOIN account a ON c.user_id = a.id WHERE c.talk_id is not null ORDER BY c.id DESC LIMIT ${start},${limit}`, async function (err, rows) {
    let comments = await Promise.all(
        rows.map(item => new Promise(resolve => {
          db.query(`select c.*,a.name from comment_reply c JOIN account a ON c.user_id = a.id WHERE replycomment_id = ${item.id}`, function (err2, rows2) {
            item.reply = rows2;
            resolve(item)
          })
        }))
    )
    db.query('select count(id) as total from comment WHERE talk_id is not null', function (err, [{total}]) {
      res.send({
        code: 200,
        message: 'success',
        comments,
        total
      })
    })
  })
})


router.post('/del', function (req, res) {
  let {id} = req.body.comment;
  let sql = '';
  if (req.body.comment.replycomment_id) {
    sql = `DELETE FROM comment_reply WHERE id =${id}`
  } else {
    sql = `DELETE FROM comment WHERE id =${id}`
  }
  db.query(sql, function (err) {
    res.send({
      code: 200,
      message: 'ok'
    })
  })
})

module.exports = router;